"use strict";

 if ("undefined" == typeof(viewSoso)) {
    var viewSoso = { }
 }
    // tag for reduce of dlg.xul\js
//    tagName     : "view-so-source:chrome,titlebar,centerscreen,modal",

 viewSoso.woman =   {
    dlg	 : null,
    open : function(amenu)
    {
        if(this.dlg)
            if(this.dlg.closed)
                this.dlg = null;

        if(this.dlg) this.dlg.focus()
        else {
            let thetag = amenu.getAttribute("title") || "";
                thetag += Date.now();
//    dump("_dvk_dbg_, menu tag:\t"); dump(thetag); dump("\n");
            this.dlg = window.openDialog( amenu.value, thetag,
                "chrome,titlebar,dependent,centerscreen,dialog",
                window.arguments[0], window.arguments[1] );
        }
//  aURL, charset, aPageDescriptor, aLineNumber, isForcedCharset);
        if(this.dlg)
        {
            window.addEventListener("close", this, false);
            window.addEventListener("activate", this, false);
        }
    },

    handleEvent: function(evt)
    {
        if(window === evt.currentTarget)
        {
            if(this.dlg)
                if(this.dlg.closed)
                    this.dlg = null;
//    dump("_dvk_dbg_, event:\t");    dump(evt.type); dump("\n");
            if(evt.type == "close") this.dlg = null;
            if(!(this.dlg)) window.removeEventListener(evt.type, this, false);
        }
        else {  //  gBrowser.addEventListener("DOMContentLoaded", 
            let theschema = "";
            if(gBrowser.contentDocument)
                theschema = gBrowser.contentDocument.documentURIObject.scheme;
            theschema = (theschema.toLowerCase().indexOf("view-source") != 0);
            let theplank = document.getElementById("notification");
                theplank.setAttribute("hidden", theschema);
//            dump(theplank.childNodes.length); dump("\n");
        }
    }
}

 viewSoso.main = {
    notnotify   : false,
    magicSchema : "HTTP",   //  only HTTP cache interests us.
//    schemeGrata : { 'about': 0, 'chrome': 1, 'file': 2, 'jar': 3, 'resource': 4 },

    //  once fired event, type: DOMContentLoaded.
    handleEvent: function( evt )
    {
        window.removeEventListener(evt.type, this, false);
        if(gBrowser)
            gBrowser.addEventListener("DOMContentLoaded", function()
                {
            let theval = Services.prefs.getBoolPref("view_source.syntax_highlight");
            document.getElementById("menu_highlightSyntax").setAttribute("checked", theval);
                }, false);        

        if(!(window.arguments)) return;
    //      dump("_dvk_dbg_, DOMContentLoaded:\n\t");
    //      dump(window.arguments[0]); dump("\n");
        var currentURI  = window.arguments[0];
        var theschema   = "";

    //	exclusive schemata ...  jar file, charset window.arguments[1]
        if(currentURI)
        try {
            currentURI = currentURI.replace(/#.*$/, ""); // cut hash
            let thuri = Services.io.newURI(currentURI, window.arguments[1], null);
                theschema = thuri.scheme.toLowerCase();
        } catch(err) {
            Components.utils.reportError(err)
        }

        //  only HTTP cache interests us.
        if(theschema.indexOf("http") === 0)
        try {
            let thecache = Services.cache.createSession(this.magicSchema, 0, true);
                thecache.doomEntriesIfExpired = false;
            this.notnotify = thecache.openCacheEntry( currentURI,
                                Components.interfaces.nsICache.ACCESS_READ,
                                false );
        }
            catch(err) {    }
        else this.notnotify = true;    //  cache hit

        if(!(this.notnotify) && (gBrowser))
            gBrowser.addEventListener("DOMContentLoaded", viewSoso.woman, false);

        return;
    },

 }
